package com.soulkun.smarthome.ui.main

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppActivityMainBinding
import com.soulkun.smarthome.logic.dto.UserMessageDTO
import com.soulkun.smarthome.logic.enum.MessageCodeEnum
import com.soulkun.smarthome.logic.manager.BroadcastManager
import soulkun.library.mvvm.component.BaseDataBindingActivity
import soulkun.library.utils.clearToast
import soulkun.library.utils.screen.ScreenUtils

class AppMainActivity : BaseDataBindingActivity<AppActivityMainBinding>() {

    companion object {

        fun startIntentAnimationDefault(activity: Activity) {
            val intent = Intent(activity, AppMainActivity::class.java)
            activity.startActivity(intent)
        }

        fun startIntentAnimationSlideVertical(activity: Activity) {
            val intent = Intent(activity, AppMainActivity::class.java)
            activity.startActivity(intent)
            activity.overridePendingTransition(R.anim.fade_in, R.anim.slide_out_down)
        }

    }

    private val userBroadcastReceiver by lazy {
        BroadcastManager.SoulkunBroadcastReceiver(MessageCodeEnum.User) {
            val userMessageDTO = it as UserMessageDTO
            val userMessageDialog = AppMainUserMessageDialog(userMessageDTO.message)
            userMessageDialog.show(supportFragmentManager, "userMessageDialog")
        }
    }

    override fun setLayout(): Int {
        return R.layout.app_activity_main
    }

    override fun initIntent(savedInstanceState: Bundle?) {
    }

    override fun initView() {
        ScreenUtils.setTranslucentStatusBar(this)
        ScreenUtils.setBlackStatusText(this)
        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.main_fragment_container_view) as NavHostFragment
        val navController = navHostFragment.navController
        requireDataBinding().mainBottomNavigationView.run {
            setupWithNavController(navController)
            itemIconTintList = null //不允许Icon图标着色
            clearToast(
                mutableListOf(
                    R.id.app_navigation_main_home,
//                    R.id.app_navigation_main_scene,
                    R.id.app_navigation_main_personal
                )
            )
            setOnNavigationItemReselectedListener {
            }
        }
    }

    override fun initData() {
    }

    override fun initListener() {
    }

    override fun onStart() {
        super.onStart()
        BroadcastManager.registerBroadcast(mContext, MessageCodeEnum.User, userBroadcastReceiver)
    }

    override fun onStop() {
        super.onStop()
        BroadcastManager.unregisterBroadcast(mContext, userBroadcastReceiver)
    }

}